/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package construtora.limiar.util;

import construtora.limiar.mensagens.Message;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/**
 *
 * @author michel
 */
public class AuthorizationController {
    
    private static AuthorizationController instance = null;
    private static Set<String> roles = new HashSet<String>();
    
    private AuthorizationController(String user){
    }
    
    public static AuthorizationController getInstance(String user){
        if (instance == null)
        {
            instance = new AuthorizationController(user);
        }
        instance.updateRoles(user);
        return instance;
    }
    
    private void updateRoles(String user) {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("authorization.properties");
        Properties properties = new Properties();
        try {
            roles.clear();
            properties.load(in);in.close();
            String[] papeisDoUsuario = properties.getProperty(user).split(",", -1);
            for (int i=0;i<papeisDoUsuario.length;i++)
            {
                roles.add(papeisDoUsuario[i]);
            }
        }catch (Exception e) {
            Message.setLastException(e.getMessage());
        }
    }
    
    
    public boolean hasRole(String[] userRoles){
        for (int i=0;i<userRoles.length;i++)
        {
            if (roles.contains(userRoles[i]))
                return true;
        }
        return false;
    }




    
}
